package com.bootcamp.javase2.synchro;

/**
 * @program: day20Sycn
 * @description: use synchronized method
 * @author: Keyu Li
 * @create: 2021-09-14 20:25
 **/

public class DownloadFile {
    public static void main(String[] args) {
        Runnable downfile = new DownFile();
        new Thread(downfile, "ThreadA").start();
        new Thread(downfile, "ThreadB").start();
        new Thread(downfile, "ThreadC").start();
    }
}

class DownFile implements Runnable {
    int fileSize = 100;

    @Override
    public void run() {
        while (true) {
            down();
        }
    }

    private synchronized void down() {
        if (fileSize > 0) {
            System.out.println(Thread.currentThread().getName()
                    + " downloading, file remaining: "
                    + fileSize-- + "MB");
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
